<?php 
session_start();
include_once "dbconnect.php";
/*include "securimage.php"; 
if(isset($_POST['submit'])) //This code will be executed only when the user click the submit
{
  $img = new Securimage();
  $valid = $img->check($_POST['code']);
}*/

/* Moved to the top. Keeps the page cleaner */
function check_email_address($email) 
{
    // First, we check that there's one @ symbol, and that the lengths are right
    if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) 
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
        return false;

// Split it into sections to make life easier
    $email_array = explode("@", $email);
    $local_array = explode(".", $email_array[0]);
    for ($i = 0; $i < sizeof($local_array); $i++) 
        if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) 
            return false;

    if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) 
    { // Check if domain is IP. If not, it should be valid domain name
        $domain_array = explode(".", $email_array[1]);
        if (sizeof($domain_array) < 2) 
            return false; // Not enough parts to domain
    }

    for ($i = 0; $i < sizeof($domain_array); $i++) 
        if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) 
            return false;

    return true;
}
		
function gen_password ()
{
    /*I and 1 removed because they look very similar. Funda stolen from django */
    static $alphanum = array('A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q',
        'R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i',
        'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','2',
        '3','4','5','6','7','8','9','0');

    $pass = "";
    for ($i=0; $i<10 ; $i++)
        $pass .= $alphanum[rand(0,59)];
    return $pass;
}

/* If you're logged in, you can't register */
if (isset($_SESSION['login'])) {
    $_POST['already_login'] = True;
    header ("Location: index.php");
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<link rel="stylesheet" type="text/css" title="My style" media="screen" href="css/style.css" />
<style type="text/css">
#checkEmail {
  margin-left: auto;
  margin-right: auto;
  margin-top: 40px;
  padding: 20px;
  width: 400px;
  height: 50px;
  background: #C0C0C0;
  -moz-border-radius: 20px;
  }
#linkback {
  margin-left: auto;
  margin-right: auto;
  margin-top: 40px;
  padding: 20px;
  width: 200px;
  height: 20px;
  background: #C0C0C0;
  -moz-border-radius: 20px;
  }
</style>
<title> ..:: mukt.in | Visitors Registration ::..</title>
<script type="text/javascript" src="js/registration.js"></script>
</head>
<body>
<?php
/*
if(isset($_POST['submit'])) {
  if($valid == true) 
    { }
   else {
    echo "<center><div id='checkEmail'>Sorry, the code you entered was invalid.  <a href='registration.php'>Go back</a> to try again.</div></center>";
    die;
  }
}*/
?>
<?php
// User may turn off Java script ,hence its better to do form validation with php i.e at serverside also .
if(isset($_POST['submit'])) //This code will be executed only when the user click the submit 
{

//no need to change the db name , password in all the files just change here .

	//This is block is to avoid sql injection 
//	$loginname = mysql_real_escape_string($_POST['loginname']);
//	$password = mysql_real_escape_string($_POST['password']); password field has been removed
	$firstname = mysql_real_escape_string($_POST['firstname']);
	$lastname = mysql_real_escape_string($_POST['lastname']);
	$email = mysql_real_escape_string($_POST['email']);
	$role = mysql_real_escape_string($_POST['role']);
	$organization = mysql_real_escape_string($_POST['organization']);
	$city = mysql_real_escape_string($_POST['city']); 
	$state = mysql_real_escape_string($_POST['state']); 
	$activationcode = strval(mt_rand());
	$status = 0 ; // its mean the user has not clicked that activation link
	$type='1';		// Telling us that this is user registration

	//To avoid null values

    /* Irony. This happens if JS is disabled. But uses JS */
	if ((trim($firstname) == "")||(trim($lastname) == "")||(trim($email) == "")||(trim($organization) == "")||(trim($city) == "")||(trim($state) == "")||(trim($role) ==""))
		die("<div id='checkEmail'>please fill all the fields <a href='javascript:history.go(-1)'>Go back</a></div>");

	if(!check_email_address($email)) 
		die("<div id='checkEmail'> " . $email . " is a not a valid email address.");
	//This block is to avoid duplicate entries 
	$query1 = "SELECT email FROM mukt_users WHERE email='$email'"; 
	//email is the best thing to identify duplicate users
	$user_exist = mysql_query($query1) or die(mysql_error());
	if($user_exist && mysql_num_rows($user_exist) >= 1) 
		die("<div id='checkEmail' align='center'>This email has already been registered. <br /> Please use another one. <br /><a href='javascript:history.go(-1)'>Go back</a>");
	  
    
	//For email address format vaildation 
		
    /* Generate a random password.*/
    $pass = gen_password();
    $query2 =	"insert into mukt_users (password, firstname, lastname, email,city,state,role, activationcode,status) 
        value (md5('$pass'), '$firstname','$lastname','$email','$city','$state','$role','$activationcode','$status')" ;
	
	mysql_query($query2) or die('insertion error ' . mysql_error());
	$headers = 'From: muktin@unixpod.com' . "\r\n" .
    'Reply-To: muktin@unixpod.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

//dont forget to change the url in the MSG 
    $message = <<<MSG
	
	
	Hi $firstname,
	
	Thank you for registering at mukt.in 2008.
	
	This is your confirmation link, please click on it to complete your registration.
	http://mukt.in/test/reg_confirm.php?activationcode=$activationcode&email=$email&type=$type

    After that, you can login using your email as a user-id, and the following password:
    Email Addres: $email
    Password: $pass
	
	Note : If the hyperlink doesn't work, please Copy and paste the url in the browser address bar.
	
	Thanks,
	mukt.in Team
MSG;

		mail( $email, "mukt.in Activation Email", $message, $headers);
		echo "<div id='checkEmail' align='center'>An activation link has been sent to <b>$email</b> <br />We request you to confirm activation.<br /><br /></div>";
		echo "<div id='linkback' align='center'><a href='index.php'>Back to Home Page</a></div>";
		
	
}

if (!isset($_POST['submit'])) {
?>
<div id="container">
<div id="header">
<br /><b></b>
</div>
<div id="leftColumn">
<div id="menucontainer">
<ul id="hex">
<li class="p1"><a class="inner" href="participate.php"><b></b><span>Participate</span><em></em></a></li>
<li><a href="sponsors.php"><b></b><span>Sponsors</span><em></em></a></li>
<li class="p2"><a href="http://groups.google.com/group/mukt/feed/rss_v2_0_msgs.xml"><b></b><span>Feeds</span><em></em></a></li>
<li class="p2"><a href="index.php"><b></b><span>About</span><em></em></a></li>
<li class="p2"><a href="studio.php"><b></b><span>Studio</span><em></em></a></li>
<li class="p1 p2"><a href="chat.php"><b></b><span>Chat</span><em></em></a></li>
<li class="p2"><a href="contact.php"><b></b><span>Contact</span><em></em></a></li>
</ul>
</div>
</div>
<div id="rightColumn">
<p align="center" id="numUsers">
<br />
<?php 
$num=mysql_num_rows(mysql_query("select * from mukt_users"));
echo "Registered Users: <span class='green'>$num</span>";
?> 
</p>
<br /><br />
<p align="center"><b>For Talks , demo , Session Registration <blink><a href="participants_registration.php">Click here</a></blink></b></p><br/> 
<p align="center"> <b>All the Field must be filled </b></p>
<form method="post" action="registration.php" name="form" id="form" onsubmit="validate(this) ; return false;">
<table cellspacing="20" cellpadding="10"> 


<!--dont change the names used here becaues its used for JS validation !--> 


<tr>
<td>First Name: </td>
<td><input type="text" name="firstname" id="firstname" /></td>
<td><img src="images/info.png" title="Enter your first name here" alt="Enter your first name here" /></td>
</tr>
 
<tr>
<td>Last Name: </td>
<td><input type="text" name="lastname" id="lastname" /></td>
<td><img src="images/info.png" title="Enter your last name here" alt="Enter your last name here" /></td>
</tr>

<tr>
<td>Email: </td>
<td><input type="text" name="email" id="email" /></td>
<td><img src="images/info.png" title="Enter your e-mail address here" alt="Enter your e-mail address here" /></td>
</tr>
 
<tr><td>Role: </td><td>
<select name="role" id="role">
<option value="">Select</option>
<option value="student">Student</option>
<option value="professional">Professional</option>
<option value="other">Other</option>
</select>
</td>
<td><img src="images/info.png" title="Select your designation " alt="Select your designation" /></td>
</tr>
 
<tr>
<td>Organization/College: </td>
<td><input type="text" name="organization" id="organization" /></td>
<td><img src="images/info.png" title="Enter your Organization and University/college name here" alt="Enter your Organization and University / college name here" /></td>
</tr>
 
<tr><td>State: </td><td>
<select name="state" id="state">
<option value="">Select
</option><option value="Andaman and Nicobar Islands">Andaman and Nicobar Islands
</option><option value="Andhra Pradesh">Andhra Pradesh 
</option><option value="Arunachal Pradesh">Arunachal Pradesh
</option><option value="Assam">Assam
</option><option value="Bihar">Bihar
</option><option value="Chandigarh">Chandigarh
</option><option value="Chhattisgarh">Chhattisgarh
</option><option value="Dadra and Nagar Haveli">Dadra and Nagar Haveli
</option><option value="Daman and Diu">Daman and Diu
</option><option value="Delhi">Delhi
</option><option value="Goa">Goa
</option><option value="Gujarat">Gujarat
</option><option value="Haryana">Haryana
</option><option value="Himachal Pradesh">Himachal Pradesh
</option><option value="Jammu and Kashmir">Jammu and Kashmir
</option><option value="Jharkhand">Jharkhand
</option><option value="Karnataka">Karnataka
</option><option value="Kerala">Kerala
</option><option value="Lakshadweep">Lakshadweep 
</option><option value="Madhya Pradesh">Madhya Pradesh
</option><option value="Maharashtra">Maharashtra
</option><option value="Manipur[1]">Manipur[1]
</option><option value="Meghalaya">Meghalaya
</option><option value="Mizoram">Mizoram
</option><option value="Nagaland">Nagaland
</option><option value="Orissa">Orissa
</option><option value="Pondicherry">Pondicherry
</option><option value="Punjab">Punjab
</option><option value="Rajasthan">Rajasthan
</option><option value="Sikkim">Sikkim
</option><option value="Tamil Nadu">Tamil Nadu
</option><option value="Tripura">Tripura
</option><option value="Uttar Pradesh">Uttar Pradesh
</option><option value="Uttarakhand">Uttarakhand
</option><option value="West Bengal">West Bengal
</option><option value="other">Other
 
</option>
</select>
</td>
 
<td><img src="images/info.png" title="Select Your state " alt="Select your state" /></td>
</tr>
 
<tr><td>City: </td><td><input type="text" name="city" id="city" /></td>
<td><img src="images/info.png" title="Select Your City " alt="Select your city" /></td></tr>

<!--<tr><td></td>
<td><img src="securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>" alt="Security Code" /><br /></td></tr>
<tr><td>Enter the security code as shown above in the image: </td>
<td><input type="text" name="code" id="code" /><br /></td></tr> -->
 
<tr><td><!-- <input type="reset" title="click here to clear the from" /> --></td>
<td><br /><input type="submit" name="submit" value="Yes, I want to Attend mukt.in 2008" title="click here to submit the form" /></td></tr> 
</table>
</form>
</div>

<div id="footer">
<div id="loginbox">
    <p>
    <?php if (isset($_SESSION['login'])) { ?>
    You are logged in as: <a href="profile.php"><?php echo $_SESSION['uname']?></a> [<a href="logout.php">Logout</a>]
    <?php } else { ?>
    <a href="login.php">[Login]</a>
    <?php } ?>
    </p>
</div>
<p>Thanks to Svein Erik Storkaas. CSS Hack by krish.</p>
</div>
</div>

<?php } //Endif not to display form if user has submitted data ?>
</body>
</html>
